package com.glamour.hlcity.component.task.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.transaction.annotation.Transactional;

import com.glamour.hlcity.common.annotation.ExecuteTarget;
import com.glamour.hlcity.common.annotation.MySqlLockAspect;
import com.glamour.hlcity.common.condition.CommandQueryCondition;
import com.glamour.hlcity.common.model.CommandDO;
import com.glamour.hlcity.common.util.DateFormatUtil;
import com.glamour.hlcity.common.util.ListUtil;
import com.glamour.hlcity.componenet.task.factory.RunnableFactory;
import com.glamour.hlcity.component.task.api.CmdManager;
import com.glamour.hlcity.component.task.model.Command;



@ExecuteTarget(executeBeanName=RunnableFactory.DAEMON_THREAD_POOL)
public class CmdCreateExecute extends AbstractCmdCreateExecute {
	private Integer cmdSize=SIZE;
	private final static String LOCK_NAME="glamour_lock.COMMAND_SELECT";
	private final static Log LOG=LogFactory.getLog(CmdCreateExecute.class);
	private final static int SECOND_2=2;
	private final static Integer ZERO=0;
	private CmdManager cmdManager;
	@Override
	@Transactional
	@MySqlLockAspect(lockName=LOCK_NAME,second=SECOND_2)
	public List<Command> createExecuteUnit() {
		try{
			CommandQueryCondition condition=new CommandQueryCondition();
			condition.addStatusList(CommandDO.STATUS_1,CommandDO.STATUS_2).addENextActDate(DateFormatUtil.date2String(new Date(), DateFormatUtil.TIME_FORMAT_A)).addLimit(ZERO, cmdSize);
			List<CommandDO> commandDOList=commandDao.selectByConditon(condition);
			//更新命令
			cmdManager.updateStatusAndNextActDate(commandDOList);
			return cmdToCommand(commandDOList);
		}catch(Exception e){
			LOG.error("获取命令发生异常",e);
			return ListUtil.emptyList();
		}
	}
	
	private List<Command> cmdToCommand(List<CommandDO> cmdList){
		List<Command> commandList=new ArrayList<Command>();
		for(CommandDO commandDO:cmdList){
			commandList.add(new Command(commandDO.getType(),commandDO.getBizNo()));
		}
		return commandList;
	}
	public Integer getCmdSize() {
		return cmdSize;
	}
	public void setCmdSize(Integer cmdSize) {
		this.cmdSize = cmdSize;
	}

	public void setCmdManager(CmdManager cmdManager) {
		this.cmdManager = cmdManager;
	}
	


	
	
	
	

}
